package org.example.websocket;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author：无敌暴龙战士
 * @Package：org.example.websocket
 * @Project：learn_websocket
 * @name：WebsocketTask
 * @Date：2025/1/13 12:56
 * @Filename：WebsocketTask
 * @Description:
 */
@Component
public class WebsocketTask {
    @Resource
    private WebSocketServer webSocketServer;

    @Scheduled(cron = "0/5 * * * * ?")
    public void sendMessageToClient() {
        String msg = "来自服务器的消息" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now());
        System.out.println("服务器发送 = " + msg);
        webSocketServer.sendToAll(msg);
    }
}
